package com.adobe.webapis.odeo
{
	import flexunit.framework.TestCase;
	import com.adobe.webapis.odeo.OdeoService;
	import com.adobe.webapis.odeo.Podcast;
	import com.adobe.webapis.odeo.events.OdeoResultEvent;

	public class OdeoServiceTest extends flexunit.framework.TestCase
	{
		private const CALL_TIMEOUT:int = 10000;
		private const USER_NAME:String = "MikeChambers";
		private const TAG_NAME:String = "Flash";
	
		public function testSearchUser():void
		{
			var o:OdeoService = new OdeoService();
				o.addEventListener(OdeoResultEvent.ON_SEARCH_USER_RESULT,
										addAsync(onGetUserSearch, CALL_TIMEOUT));
				o.searchUser(USER_NAME);
		}
	
		private function onGetUserSearch(event:OdeoResultEvent):void
		{
			var a:Array = event.data.podcasts;
			
			assertNotNull("a is undefined", a);
			assertTrue("a.length > 0", a.length > 0);
			assertTrue("a[0] is Podcast",a[0] is Podcast);
			
			assertNotNull("event.data.searchTerm is undefined",
														event.data.searchTerm);
											
			assertTrue("event.data.searchTerm == USER_NAME",
											event.data.searchTerm == USER_NAME);
		}
	
		public function testSearchTag():void
		{
			var o:OdeoService = new OdeoService();
				o.addEventListener(OdeoResultEvent.ON_SEARCH_TAG_RESULT,
										addAsync(onGetTagSearch, CALL_TIMEOUT));
				o.searchTag(TAG_NAME);
		}
	
		private function onGetTagSearch(event:OdeoResultEvent):void
		{
			var a:Array = event.data.podcasts;
			
			assertNotNull("a is undefined", a);
			assertTrue("a.length > 0", a.length > 0);
			assertTrue("a[0] is Podcast",a[0] is Podcast);
			
			assertNotNull("event.data.searchTerm is undefined",
														event.data.searchTerm);
											
			assertTrue("event.data.searchTerm == TAG_NAME",
											event.data.searchTerm == TAG_NAME);
		}	
	
		public function testParseFeed():void
		{
			var o:OdeoService = new OdeoService();
			var arr:Array = o.parseFeed(OdeoFeed.FEED);
			
			assertNotNull("arr is null", arr);
			assertTrue("arr.length == OdeoFeed.FEED_LENGTH",
										arr.length == OdeoFeed.FEED_LENGTH);
										
			var pc:Podcast = arr[0];							
			assertTrue("pc is Podcast", pc is Podcast);
			assertTrue("pc.author == \"boagworld.com\"", 
												pc.author == "boagworld.com");
			assertTrue("pc.contentType == \"audio/mpeg\"",
												pc.contentType == "audio/mpeg");
			assertTrue("pc.size == 17367625", pc.size == 17367625);
				
			assertNotNull("pc.description ", pc.description);
			
			assertTrue("pc.keywords.length == 6", pc.keywords.length == 6);
			
			var keywords:Array = ["Director", "macromedia", "flash", "stats",
								 "web", "stats"];
			
			var kLen:int = keywords.length;
			
			for(var i:int = 0; i < kLen; i++)
			{
				assertTrue("keywords["+i+"] == pc.keywords["+i+"]",
												keywords[i] == pc.keywords[i]);
			}
			
			assertTrue("pc.link == \"http://odeo.com/show/648397/view\"",
								pc.link == "http://odeo.com/show/648397/view");
								
			//should i deserialize this and compare?
			assertNotNull("pc.publishDate is null", pc.publishDate);
			
			assertTrue("pc.title == \"21. Working with stats\"",
											pc.title == "21. Working with stats");
											
			assertTrue("pc.url == \"http://odeo.com/show/download/648397/4/www.esanity.co.uk.podcasts.boagworld21.mp3\"",
								pc.url == "http://odeo.com/show/download/648397/4/www.esanity.co.uk.podcasts.boagworld21.mp3");
			
		}
		
	}
}